package com.tanklab.signature.controller;


import com.tanklab.signature.common.util.JwtUtils;
import com.tanklab.signature.common.util.ResultCode;
import com.tanklab.signature.ds.req.UserLoginReq;
import com.tanklab.signature.ds.req.UserProfileUpdateReq;
import com.tanklab.signature.ds.req.UserRegisterReq;
import com.tanklab.signature.ds.resp.CommonResp;
import com.tanklab.signature.service.UserService;
import com.tanklab.signature.service.WeChatAuthService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


@RestController
@RequestMapping("/signature/user")
@ApiModel
public class UserController {
    @Autowired
    UserService userService;

    @Autowired
    WeChatAuthService weChatAuthService;

    @Autowired
    private JwtUtils jwtUtils;

    // Deprecated
    @ApiOperation(value = "用户登录，前端传入账号密码")
    @PostMapping("/login")
    public CommonResp Login(@RequestBody UserLoginReq userloginreq) throws Exception {
        return userService.login(userloginreq);
    }

    // Deprecated
    @ApiOperation(value = "用户注册，前端传入账号密码")
    @PostMapping("/register")
    public CommonResp Register(@RequestBody UserRegisterReq userregisterreq) throws Exception {
        return userService.register(userregisterreq);
    }


    @ApiOperation(value = "刷新访问Token")
    @PostMapping("/auth/refresh-token")
    public CommonResp refreshToken(@RequestBody Map<String, String> requestBody) {
        String refreshToken = requestBody.get("refreshToken");
        return weChatAuthService.refreshToken(refreshToken);
    }


    @ApiOperation(value = "微信小程序登录")
    @PostMapping("/auth/login")
    public CommonResp AuthLogin(@RequestBody Map<String, String> requestBody) throws Exception {
        String code = requestBody.get("code");
        System.out.println("code: " + code);
        return weChatAuthService.login(code);
    }

    // just for test
    @GetMapping("/auth/check")
    public CommonResp checkAuth()  {
        CommonResp resp = new CommonResp();
        resp.setRet(ResultCode.SUCCESS);
        resp.setData(true);
        return resp;
    }

    // Deprecated
//    @ApiOperation(value = "退出登录")
//    @PostMapping("/auth/logout")
//    public CommonResp logout(HttpServletRequest request){
//        return weChatAuthService.logout(request);
//    }

    @ApiOperation(value = "更新用户基本资料")
    @PostMapping("/update-profile")
    public CommonResp updateProfile(@RequestBody UserProfileUpdateReq profileReq, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (token.startsWith("Bearer ")) {
            token = token.substring(7);
        }

        // 从Token提取openid
        String openid = jwtUtils.getOpenidFromToken(token);
        System.out.println("openid: " + openid);
        System.out.println("profileReq: " + profileReq);

        // 调用Service更新用户资料
        return userService.updateUserProfile(profileReq, openid);
    }

    @ApiOperation(value = "获取用户信息")
    @GetMapping("/profile/get")
    public CommonResp getUserProfile(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (token.startsWith("Bearer ")) {
            token = token.substring(7);
        }

        // 从Token提取openid
        String openid = jwtUtils.getOpenidFromToken(token);

        // 调用Service获取用户资料
        return userService.getUserProfile(openid);
    }

}